package com.framework.bdf4j.job.service.impl;

import java.util.Calendar;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.framework.bdf4j.job.entity.SysJobLog;
import com.framework.bdf4j.job.mapper.SysJobLogMapper;
import com.framework.bdf4j.job.service.SysJobLogService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 定时任务执行日志表
 *
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements SysJobLogService {

    /**
     * 分页查询日志信息
     *
     * @param page
     *            分页对象
     * @param joblog
     *            参数列表
     * @return
     */
    @Override
    public IPage getJobLogPage(Page page, SysJobLog joblog) {
    	QueryWrapper<SysJobLog> ew = new QueryWrapper<SysJobLog>();
    	ew.eq(StrUtil.isNotBlank(joblog.getJobLogStatus()), "job_log_status", joblog.getJobLogStatus())
    	.likeRight(StrUtil.isNotBlank(joblog.getJobName()), "job_name", joblog.getJobName())
    	.likeRight(StrUtil.isNotBlank(joblog.getJobGroup()), "job_group", joblog.getJobGroup());
    	try {
    		if(StrUtil.isNotBlank(joblog.getBeginTime()))
    			ew.ge("create_time", DateUtil.parseDate(joblog.getBeginTime()));
    		if(StrUtil.isNotBlank(joblog.getEndTime())) {
    			Date date=DateUtil.parseDate(joblog.getEndTime());
    			Calendar calendar=Calendar.getInstance();
    			calendar.setTime(date);
    			calendar.add(Calendar.DATE, 1);
    			ew.lt("create_time", calendar.getTime());
    		}
		} catch (Exception e) {
			log.error("时间转换错误:",e);
		}
        return baseMapper.getJobLogPage(page, ew);
    }
}
